<?php
/*
Template Name: Target
*/
?><?php get_header(); ?>
<?php
//set up display variables
$max_feat = 0;
$max_post = 0;
$full = true;
$more = false;

$qtopic = '';
$qaiclass = '';
$msg = '';

//handle get requests for topic and ai_class, ai_class is nested in previous topic
if (isset($_GET['topic'])){
    $qtopic = $_GET['topic'];
    set_transient('mct_ai_lasttopic',$qtopic,60*60);
}
elseif (isset($_GET['ai_class'])){
    $qaiclass = $_GET['ai_class'];
}
else {
    set_transient('mct_ai_lasttopic','');
}
    
$temp = $wp_query;
$wp_query= null;

$q_args = array(
    'post_type' => 'target_ai',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 10,
    'paged' => $paged
);
//Add topic, class query args
$msg = "Featured Target";
if (!empty($qtopic)){
    $qterm = get_term_by('slug',$qtopic,'topic');
    $q_args['topic'] = $qtopic;
    $msg = "Topic &raquo; ".$qterm->name;
}
if (!empty($qaiclass)){
    $lasttopic=get_transient('mct_ai_lasttopic');
    if (!empty($lasttopic)){
        $qterm = get_term_by('slug',$lasttopic,'topic');
        $q_args['topic'] = $lasttopic;
        $q_args['ai_class'] = $qaiclass;
        $msg = "Topic &raquo; ".$qterm->name." &middot Relevance &raquo; ".$qaiclass;
    } else {
        $q_args['ai_class'] = $qaiclass;
        $msg = "Relevance &raquo; ".$qaiclass;
    }
}
$wp_query = new WP_Query($q_args);

?>
<div id="content" class="archive">
    
    <?php if (have_posts()) : $i = 1; // the loop ?>

    <h2><?php echo $msg; ?></h2>

    <?php while (have_posts()) : the_post(); // the loop ?>
    
    <?php if($i == 1) : ?>
    <div id="feature" class="clearfix">
		<?php image_attachment('mct_sl_image', '210', '140'); ?>
	    <h3 class="title"><?php the_title(); ?></h3>
	    <div class="meta"><?php the_time('F j, Y') ?>
            <?php edit_post_link( '[Edit]', '', ''); do_action('prox_meta_archive',$post->ID); 
            echo(get_the_term_target_ai($post->ID,'topic',' &middot ',',',' '));
            echo(get_the_term_target_ai($post->ID,'ai_class',' &middot ',',',' ')); ?></em></div>
	    <p><?php prox_content_limit($max_feat,'More...'); ?></p>
    </div>
 
    <h2>More Targets</h2>
    <?php else : ?>
	    <div class="post clearfix">
	    <?php image_attachment('mct_sl_image', '100', '100'); ?>
	    <h3 class="title"><?php the_title(); ?></h3>
	    <div class="meta"><?php the_time('F j, Y') ?>
            <?php edit_post_link( '[Edit]', '', ''); do_action('prox_meta_archive',$post->ID); 
            echo(get_the_term_target_ai($post->ID,'topic',' &middot ',',',' '));
            echo(get_the_term_target_ai($post->ID,'ai_class',' &middot ',',',' ')); ?></em></div>
	    <p><?php prox_content_limit($max_post,'More...'); ?></p>
            <?php if ($full){
               echo '<div class="meta"><p><em>'; the_tags('Tagged as: ',',',''); echo '</em></p></div>';
            } ?>  
            
            </div><!--end .post-->
    <?php endif; ?>

    <?php $i = $i + 1; endwhile; //end one post ?>
    
    <!-- Previous/Next page navigation -->
    <div class="page-nav">
	    <div class="nav-previous"><?php previous_posts_link(__('&larr; Previous Page')) ?></div>
	    <div class="nav-next"><?php next_posts_link(__('Next Page &rarr;')); ?></div>
    </div>   
                
	<?php else : //do not delete ?>

    <h3><?php _e("Page Not Found"); ?></h3>
    <p><?php _e("We're sorry, but the page you are looking for isn't here."); ?></p>
    <p><?php _e("Try searching for the page you are looking for or using the navigation in the header or sidebar"); ?></p>

	<?php endif; //do not delete ?>
	<?php $wp_query = null; $wp_query = $temp;?>	
</div><!--end #content-->

<!--include sidebar-->
<?php include('sidebar-target.php'); ?>

<!--include footer-->
<?php get_footer(); ?>